'use strict';

const Service = require('egg').Service;

// const { attachPaginate } = require('knex-paginate');
// attachPaginate();
class HomeService extends Service {

    /**
   * 推荐
   */
  async recommend(paging) {
    const { app } = this;
    const {[0]:plan} = await app.knex.raw(`select * from pp_cms_products where flags LIKE '%1%' ORDER BY dateline ASC LIMIT ${(paging.currentPage-1)*paging.perPage}, ${paging.perPage}`);
    const {[0]:pages} = await app.knex.raw(`select count(*) AS count from pp_cms_products where flags LIKE '%1%'`)
    let count = pages[0].count
    let totalPages =await this.service.util.paging(paging,count)
    let tempo =[]
    for(let arr of plan){
        let user =await this.service.user.getUser(arr.uid)
        let avatar = ''
        if(user){
            avatar = user.avatar
        }else{
            avatar = ''
        }
        tempo.push(
            {
                id:arr.id,
                title:arr.title,
                tags:arr.tags,
                pic:arr.pic,
                price:arr.price,
                flags:arr.flags,
                uid: arr.uid,
                author: arr.author,
                avatar,
            }
        )
    }
    return {tempo,totalPages}
  }

    /**
   * 个案
   */
  async plan(paging) {
    const { app } = this;
    const {[0]:plan} = await app.knex.raw(`select * from pp_cms_products WHERE cid != '30' and cid != '1' and cid != '33' LIMIT ${(paging.currentPage-1)*paging.perPage}, ${paging.perPage}`);
    const {[0]:pages} = await app.knex.raw(`select count(*) AS count from pp_cms_products WHERE cid != '30' and cid != '1' and cid != '33'`)
    let count = pages[0].count
    let totalPages =await this.service.util.paging(paging,count)
    let tempo =[]
    for(let arr of plan){
        let user =await this.service.user.getUser(arr.uid)
        let avatar = ''
        if(user){
            avatar = user.avatar
        }else{
            avatar = ''
        }
        tempo.push(
            {
                id:arr.id,
                title:arr.title,
                tags:arr.tags,
                pic:arr.pic,
                price:arr.price,
                flags:arr.flags,
                uid: arr.uid,
                author: arr.author,
                avatar,
            }
        )
    }
    return {tempo,totalPages}
}
  /**
   * 全案
   */
async fullService(paging) {
    const { app } = this;
    const {[0]:plan} = await app.knex.raw(`select * from pp_cms_products WHERE cid = '30' LIMIT ${(paging.currentPage-1)*paging.perPage}, ${paging.perPage}`);
    const {[0]:pages} = await app.knex.raw(`select count(*) AS count from pp_cms_products WHERE cid = '30'`)
    let count = pages[0].count
    let totalPages =await this.service.util.paging(paging,count)
    let tempo =[]
    for(let arr of plan){
        let user =await this.service.user.getUser(arr.uid)
        let avatar = ''
        if(user){
            avatar = user.avatar
        }else{
            avatar = ''
        }
        tempo.push(
            {
                id:arr.id,
                title:arr.title,
                tags:arr.tags,
                pic:arr.pic,
                price:arr.price,
                flags:arr.flags,
                uid: arr.uid,
                author: arr.author,
                avatar
            }
        )
    }
    return {tempo,totalPages}
}
   /**
   * 专题
   */
async project(paging) {
    const { app } = this;
    const {[0]:plan} = await app.knex.raw(`select * from pp_cms_products WHERE cid = '1' LIMIT ${(paging.currentPage-1)*paging.perPage}, ${paging.perPage}`);
    const {[0]:pages} = await app.knex.raw(`select count(*) AS count from pp_cms_products WHERE cid = '1'`)
    let count = pages[0].count
    let totalPages =await this.service.util.paging(paging,count)
    let tempo =[]
    for(let arr of plan){
        let user =await this.service.user.getUser(arr.uid)
        let avatar = ''
        if(user){
            avatar = user.avatar
        }else{
            avatar = ''
        }
        tempo.push(
            {
                id:arr.id,
                title:arr.title,
                tags:arr.tags,
                pic:arr.pic,
                price:arr.price,
                flags:arr.flags,
                uid: arr.uid,
                author: arr.author,
                avatar,
            }
        )
    }
    return {tempo,totalPages}
  }
     /**
   * 调研
   */
async research(paging) {
    const { app } = this;
    const {[0]:plan} = await app.knex.raw(`select * from pp_cms_products WHERE cid = '33' LIMIT ${(paging.currentPage-1)*paging.perPage}, ${paging.perPage}`);
    const {[0]:pages} = await app.knex.raw(`select count(*) AS count from pp_cms_products WHERE cid = '33'`)
    let count = pages[0].count
    let totalPages =await this.service.util.paging(paging,count)
    let tempo =[]
    for(let arr of plan){
        let user =await this.service.user.getUser(arr.uid)
        let avatar = ''
        if(user){
            avatar = user.avatar
        }else{
            avatar = ''
        }
        tempo.push(
            {
                id:arr.id,
                title:arr.title,
                tags:arr.tags,
                pic:arr.pic,
                price:arr.price,
                flags:arr.flags,
                uid: arr.uid,
                author: arr.author,
                avatar,
            }
        )
    }
    return {tempo,totalPages}
  }
  /**
   * 问答
   */
  async registered() {
    const { ctx } = this;
  } 

      /**
   * 文章
   */
  async article(paging) {
    const { app } = this;
    const {[0]:plan} = await app.knex.raw(`select * from pp_cms_article LIMIT ${(paging.currentPage-1)*paging.perPage}, ${paging.perPage}`);
    const {[0]:pages} = await app.knex.raw(`select count(*) AS count from pp_cms_article`)
    let count = pages[0].count
    let totalPages =await this.service.util.paging(paging,count)
    let tempo =[]
    for(let arr of plan){
        let user =await this.service.user.getUser(arr.uid)
        let avatar = ''
        if(user){
            avatar = user.avatar
        }else{
            avatar = ''
        }
        tempo.push(
            {
                id:arr.id,
                title:arr.title,
                tags:arr.tags,
                pic:arr.pic,
                uid: arr.uid,
                author: arr.author,
                avatar,
            }
        )
    }
    return {tempo,totalPages}
}
}

module.exports = HomeService;
